package com.jy.datapipeline.export.controller;

import com.jy.datapipeline.export.entity.ExpTaskModel;
import com.jy.datapipeline.export.service.DataStoreInfoService;
import com.jy.datapipeline.export.service.ExpTaskService;
import com.jy.datapipeline.export.entity.DataStoreInfoModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/datastore/")
public class DataStoreInfoController {
    @Autowired
    private DataStoreInfoService dataStoreInfoService;
    @Autowired
    private ExpTaskService expTaskService;

    @RequestMapping( value = "findByList",method = RequestMethod.GET)
    @ResponseBody
    public Object findDataStoreByList(
        @RequestParam(value = "storeName",required = true) String storeName,
        @RequestParam(value = "storeType",required = true) String storeType,
        @RequestParam(value = "iDisplayStart",required = true) Integer iDisplayStart,
        @RequestParam(value = "iDisplayLength",required = true) Integer iDisplayLength
    ){
        Map<String, Object> map=new HashMap<>();

        List<Map<String,Object>> list=this.dataStoreInfoService.findDataStoreInfoByList(storeName,storeType,iDisplayStart,iDisplayLength);
        Integer count=this.dataStoreInfoService.findDataStoreInfoByCount(storeName,storeType);
        map.put("datalist",list);
        map.put("count", count);
        return map;
    }

    @RequestMapping( value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(
        @RequestParam(value = "storeName",required = true) String storeName,
        @RequestParam(value = "storeType",required = true) String storeType,
        @RequestParam(value = "host",required = true) String host,
        @RequestParam(value = "password",required = true) String password,
        @RequestParam(value = "remotePath",required = true) String remotePath,
        @RequestParam(value = "username",required = true) String username
    ){
        Map<String, Object> result=new HashMap<>();
        try{
            String storeId= "store_"+String.valueOf(System.currentTimeMillis());
            DataStoreInfoModel dsim=new DataStoreInfoModel() ;
            dsim.setHost(host);
            dsim.setStoreId(storeId);
            dsim.setPassword(password);
            dsim.setStoreName(storeName);
            dsim.setStoreType(storeType);
            dsim.setRemotePath(remotePath);
            dsim.setUserName(username);
            DataStoreInfoModel dsimInfo= this.dataStoreInfoService.findByStoreName(storeName);
            if(dsimInfo==null){
                this.dataStoreInfoService.saveDataStoreInfo(dsim);
                result.put("status",true);
                result.put("msg","success");
            }else {
                result.put("status",false);
                result.put("msg","存储名称重复");
            }
        }catch (Exception e){
            log.error("add exception",e);
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(
        @RequestParam(value = "storeName",required = true) String storeName,
        @RequestParam(value = "storeType",required = true) String storeType,
        @RequestParam(value = "host",required = true) String host,
        @RequestParam(value = "password",required = true) String password,
        @RequestParam(value = "remotePath",required = true) String remotePath,
        @RequestParam(value = "username",required = true) String username,
        @RequestParam(value = "storeId",required = true) String storeId
    ){
        Map<String, Object> result=new HashMap<>();
        try{
            DataStoreInfoModel dsim=new DataStoreInfoModel();
            dsim.setHost(host);
            dsim.setStoreId(storeId);
            dsim.setPassword(password);
            dsim.setStoreName(storeName);
            dsim.setStoreType(storeType);
            dsim.setRemotePath(remotePath);
            dsim.setUserName(username);
            DataStoreInfoModel dsimInfo= this.dataStoreInfoService.findByStoreId(storeId);
            boolean isUpdate=false;
            if(dsimInfo!=null){
                if(!storeName.equals(dsimInfo.getStoreName())){
                    DataStoreInfoModel dsimInfo2= this.dataStoreInfoService.findByStoreName(storeName);
                    if(dsimInfo2!=null){
                        result.put("status",false);
                        result.put("msg","存储名称重复");
                        return result;
                    }else{
                        isUpdate=this.dataStoreInfoService.updateDataStoreInfo(dsim);
                    }

                }else{
                    isUpdate=this.dataStoreInfoService.updateDataStoreInfo(dsim);
                }

                result.put("status",isUpdate);
                result.put("msg",isUpdate?"success":"修改失败");
            }else{
                result.put("status",false);
                result.put("msg","存储信息不存在");
            }

        }catch (Exception e){
            log.error("update exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "deleteById",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteById(
        @RequestParam(value = "id",required = true) int id
    ){
        Map<String, Object> result=new HashMap<>();
        try{
         DataStoreInfoModel dataStoreInfoModel=   this.dataStoreInfoService.findById(id);
         if(dataStoreInfoModel!=null){
             List<ExpTaskModel> taskModelList=  expTaskService.findByStoreId(dataStoreInfoModel.getStoreId());
             if(taskModelList.size()==0){
                 boolean flag=this.dataStoreInfoService.deleteDataStoreInfoById(id);
                 result.put("status",flag);
                 result.put("msg","success");
             }else{
                 result.put("status",false);
                 result.put("msg"," 请先删除关联的导出任务");
             }
         }else {
             result.put("status",false);
             result.put("msg"," 存储信息不存在");
         }

        }catch (Exception e){
            log.error("deleteById exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }
    @RequestMapping( value = "findAll",method = RequestMethod.GET)
    @ResponseBody
    public Object findAll(
    ){
        Map<String, Object> result=new HashMap<>();
        try{

            List<Map<String,Object>> list= this.dataStoreInfoService.findDataStoreInfoByList(null,null,-1,-1);
            result.put("data",list);
            result.put("status",true);
            result.put("msg","success");
        }catch (Exception e){
            log.error("findAll exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "findByStoreId",method = RequestMethod.GET)
    @ResponseBody
    public Object findAll(
        @RequestParam(value = "storeId",required = true) String  storeId
    ){
        Map<String, Object> result=new HashMap<>();
        try{

            DataStoreInfoModel dsi= this.dataStoreInfoService.findByStoreId(storeId);
            result.put("data",dsi);
            result.put("status",true);
            result.put("msg","success");
        }catch (Exception e){
            log.error("findAll exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }
}
